/*
 * FNISVGEditor - JavaScript - based editor for SVG graphics 
 * Copyright (C) 2008 - 2009 Antti Leppä / Foyt
 * http://www.foyt.fi
 * 
 * License: 
 * 
 * Licensed under GNU Lesser General Public License Version 2.1 or later (the "LGPL") 
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * Most of the color conversion algorithms used here are from GIMP's (www.gimp.org) source code. 
 * 
 * To be more specific HSV to RGB conversion is from from libgimpcolor/gimpcolorspace.c 
 */
FNISVGEditorColorHSV = Class.create({
  initialize: function(h,s,v,a) {
    this._h = h;
    this._s = s;
    this._v = v;
    this._a = a;
  },
  setHue: function (hue) {
    this._h = hue;
  },
  setSaturation: function (saturation) {
    this._s = saturation;
  },
  setValue: function (value) {
    this._v = value;
  },
  setAlpha: function (alpha) {
    this._a = alpha;    
  },
  getHue: function () {
    return this._h;
  },
  getSaturation: function () {
    return this._s;
  },
  getValue: function () {
    return this._v;
  },
  getAlpha: function () {
    return this._a;
  },
  toRGB: function () {
    var i;
    var f, w, q, t;
    var hue;
    
    var r, g, b;
    
    if (this._s == 0) {
      r = this._v;
      g = this._v;
      b = this._v;
    } else {
      hue = this._h;

      if (hue == 1.0)
        hue = 0.0;

      hue *= 6.0;

      i = Math.floor(hue);
      f = hue - i;
      w = this._v * (1.0 - this._s);
      q = this._v * (1.0 - (this._s * f));
      t = this._v * (1.0 - (this._s * (1.0 - f)));

      switch (i) {
        case 0:
          r = this._v;
          g = t;
          b = w;
          break;
        case 1:
          r = q;
          g = this._v;
          b = w;
          break;
        case 2:
          r = w;
          g = this._v;
          b = t;
          break;
        case 3:
          r = w;
          g = q;
          b = this._v;
          break;
        case 4:
          r = t;
          g = w;
          b = this._v;
          break;
        case 5:
          r = this._v;
          g = w;
          b = q;
          break;
        }
    }
    
    return new FNISVGEditorColorRGB(r, g, b, this._a);
  }
});